package com.sgb.knowledgeshare.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.entity.VideoCommentListEntity;
import com.sgb.knowledgeshare.view.activity.ShareUserActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.List;


/**
 * Created by：冯齐 on 2020/11/13 16:36
 * Description：
 */
public class SecondLevelCommentAdapter extends BaseRecycleAdapter<VideoCommentListEntity.RecordsBeanX.RecordsBean> {

    private ActionCallback callback;
    private int tempSize;
    public void setCallback(ActionCallback callback) {
        this.callback = callback;
    }
    int type;
    private String ids;

    public SecondLevelCommentAdapter(Context context, List<VideoCommentListEntity.RecordsBeanX.RecordsBean> datas, int tempSize, int type, String id) {
        super(context, R.layout.secondlevel_comment_item, datas);
        this.tempSize = tempSize;
        this.type=type;
        this.ids = id;
    }

    @Override
    public int getItemCount() {
        return tempSize == -1 ? datas.size() : tempSize;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        TextView commentTwoReply = holder.itemView.findViewById(R.id.comment_two_reply);//回复
        LinearLayout llTwoComment = holder.itemView.findViewById(R.id.ll_two_comment);
        LinearLayout llTwoCommentZan = holder.itemView.findViewById(R.id.ll_two_comment_zan);//二级点赞布局
        ImageView commentTwoLogo = holder.itemView.findViewById(R.id.comment_two_loge);//头像
        TextView commentTwoName1 = holder.itemView.findViewById(R.id.comment_two_name1);//名称1
        TextView commentTwoName2 = holder.itemView.findViewById(R.id.comment_two_name2);//名称2
        TextView author = holder.itemView.findViewById(R.id.author);//名称2
        TextView commentTwoContent = holder.itemView.findViewById(R.id.comment_two_content);//内容
        TextView commentTwoTime = holder.itemView.findViewById(R.id.comment_two_time);//时间
        ImageView imTwoCommentZan = holder.itemView.findViewById(R.id.im_two_comment_zan);//点赞布局
        TextView commentTwoZanNum = holder.itemView.findViewById(R.id.comment_two_zan_num);//点赞数量
        //二级头像
        Glide.with(context).load(datas.get(position).getImg() + "")
                .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.share_def_photo))
                .into(commentTwoLogo);
        //跳转到用户中心界面
        commentTwoLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareUserActivity.start(context, datas.get(position).getUser());
            }
        });
        //二级名称
        commentTwoName1.setText(datas.get(position).getUserName() + "");
        commentTwoName2.setText(datas.get(position).getReceiveUserName() + "");
        //判断是否是作者
        if (datas.get(position).getIsOwner() == 1) {
            author.setVisibility(View.VISIBLE);
        } else {
            author.setVisibility(View.GONE);
        }

        //二级评论内容
        commentTwoContent.setText(datas.get(position).getContent() + "");
        //时间
        commentTwoTime.setText(DateUtils.getDayTimeUtils(datas.get(position).getCreateTime()) + "");
        //是否点赞
        if (datas.get(position).getIsLike()==2){
            imTwoCommentZan.setImageResource(R.mipmap.share_comment_yes_zan);
            commentTwoZanNum.setTextColor(context.getResources().getColor(R.color.color_FF3960));
        }else {
            imTwoCommentZan.setImageResource(R.mipmap.share_comment_no_zan);
            commentTwoZanNum.setTextColor(context.getResources().getColor(R.color.color_tab_two));
        }
        //二级点赞数量
        commentTwoZanNum.setText(datas.get(position).getCommentLaudCount()+"");
        if (type==2){
            if (position==0){
                if (ids.equals(datas.get(position).getId())){
                    llTwoComment.setBackgroundColor(context.getResources().getColor(R.color.color_main_bg));
                }else {
                    llTwoComment.setBackgroundColor(context.getResources().getColor(R.color.white));
                }

            }else {
                llTwoComment.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
        }
        //长按实现删除和取消界面实现
        llTwoComment.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (callback != null) {
                    callback.onChildLongClick(datas.get(position), position);
                }
                return false;
            }
        });
        //二级回复
        commentTwoReply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) {
                    callback.onChildRely(datas.get(position), position);
                }
            }
        });
        //二级点赞
        llTwoCommentZan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) {
                    callback.onChildPraise(datas.get(position), position);
                }
            }
        });
    }

    @Override
    public void convert(ItemViewHolder holder, VideoCommentListEntity.RecordsBeanX.RecordsBean recordsBean) {

    }

    public interface ActionCallback {
        void onChildLongClick(VideoCommentListEntity.RecordsBeanX.RecordsBean holder, int position);

        void onChildRely(VideoCommentListEntity.RecordsBeanX.RecordsBean holder, int position);

        void onChildPraise(VideoCommentListEntity.RecordsBeanX.RecordsBean holder, int position);
    }

}
